import pygame
import random


class Ball:
    def __init__(self, x, y, radius, color, speed_x, speed_y):
        self.x = x
        self.y = y
        self.radius = radius
        self.color = color
        self.speed_x = speed_x
        self.speed_y = speed_y

    def move(self, screen_width, screen_height):
        self.x += self.speed_x
        self.y += self.speed_y

        if self.x - self.radius <= 0 or self.x + self.radius >= screen_width:
            self.speed_x = -self.speed_x
        if self.y - self.radius <= 0 or self.y + self.radius >= screen_height:
            self.speed_y = -self.speed_y

    def draw(self, screen):
        pygame.draw.circle(screen, self.color, (self.x, self.y), self.radius)


class Player:
    def __init__(self, x, y, width, height, color):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.color = color

    def move(self, keys, screen_width, screen_height):
        if keys[pygame.K_UP] and self.y - self.height / 2 > 0:
            self.y -= 5
        elif keys[pygame.K_DOWN] and self.y + self.height / 2 < screen_height:
            self.y += 5
        elif keys[pygame.K_LEFT] and self.x - self.width / 2 > 0:
            self.x -= 5
        elif keys[pygame.K_RIGHT] and self.x + self.width / 2 < screen_width:
            self.x += 5

    def draw(self, screen):
        pygame.draw.rect(screen, self.color, (self.x - self.width / 2, self.y - self.height / 2, self.width, self.height))

class Game:
    def __init__(self, width, height):
        pygame.init()
        self.width = width
        self.height = height
        self.screen = pygame.display.set_mode((width, height))
        pygame.display.set_caption("躲避小球游戏")
        self.clock = pygame.time.Clock()

        self.player = Player(width // 2, height - 50, 100, 20, (0, 255, 0))
        self.balls = []
        for _ in range(5):
            self.add_ball()

        self.score = 0

    def add_ball(self):
        radius = random.randint(10, 30)
        x = random.randint(radius, self.width - radius)
        y = random.randint(radius, self.height - radius)
        color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
        speed_x = random.choice([-3, 3])
        speed_y = random.choice([-3, 3])
        ball = Ball(x, y, radius, color, speed_x, speed_y)
        self.balls.append(ball)

    def run(self):
        running = True
        while running:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    running = False

            keys = pygame.key.get_pressed()
            self.player.move(keys, self.width, self.height)

            for ball in self.balls:
                ball.move(self.width, self.height)
                # if (self.player.x - self.player.width / 2 <= ball.x <= self.player.x + self.player.width / 2 and
                #         self.player.y - self.player.height / 2 <= ball.y <= self.player.y + self.player.height / 2):
                #     running = False
                if ball.y + ball.radius >= self.height:
                    self.score += 1
                    self.balls.remove(ball)
                    self.add_ball()

            self.screen.fill((0, 0, 0))
            self.player.draw(self.screen)
            for ball in self.balls:
                ball.draw(self.screen)

            font = pygame.font.Font(None, 36)
            text = font.render(f"Score: {self.score}", 1, (255, 255, 255))
            self.screen.blit(text, (10, 10))

            pygame.display.flip()
            self.clock.tick(60)

        pygame.quit()


if __name__ == "__main__":
    game = Game(800, 600)
    game.run()
